//API 操作
const API = require('../../../datas/common.js');
let app = getApp();

//创建精选页面对象
Page({
    data: {
        maxImageSize: 3,
        isHideUpload: false,
        canCreate: false,
        images: [],
        imagesUploaded: [],
        createText: '马上发布',
        contentMinLength: 5,
        contentMaxLength: 200,
        detail: {
            authorName: '',
            avatarUrl: '',
            content: '',
            description: '',
            type: 'NEARBY',
            latitude: 0,
            longitude: 0,
            images: ''
        }
    },
    onLoad: function () {
        this.data.detail.authorName = getApp().globalData.userInfo.nickname;
        this.data.detail.avatarUrl = getApp().globalData.userInfo.avatarUrl;
    },
    getContent: function (e) {
        let that = this;
        let content = e.detail.value
        that.data.detail.content = content;
        that.setData({
           canCreate: content.length >= that.data.contentMinLength
        })
    },
    create: function () {
        let that = this;
        that.setData({
            canCreate: false,
            createText: '正在发布'
        })
        API.getLocation().then((res) => {
            that.data.detail.latitude = res.latitude;
            that.data.detail.longitude = res.longitude;
            that.data.detail.images = JSON.stringify(that.data.imagesUploaded);
            that.data.detail.description = that.data.detail.length > 100 ? (that.data.detail.content.substring(0, 100) + '...') : that.data.detail.content;
            API.post('/content/nearby', that.data.detail).then(res => {
                app.setNearbyData([res.data]);
                wx.navigateBack({delta: 1});
            }).catch((res) => {
                API.showToast(res.message);
            })
        }).catch((res) => {
            API.showToast(res.message);
        });
    },
    uploadImage: function () {
        wx.chooseImage({
            count: this.data.maxImageSize - this.data.images.length,
            sizeType: ['original'],
            sourceType: ['album', 'camera'],
            success: (res) => {
                let filePaths = res.tempFilePaths;
                let images = this.data.images;
                let index = images.length;
                for (let i = 0; i < filePaths.length; i++) {
                    images = images.concat({'imageUrl': filePaths[i], 'status': '准备上传'});
                }
                if (this.data.maxImageSize == images.length) {
                    this.setData({
                        isHideUpload: true,
                    });
                }
                this.setData({
                    images: images,
                    canCreate: false
                });
                this.batchUpload(filePaths, index);
            },
        });
    },
    batchUpload(filePaths, index){
        let that = this;
        let uploadedImage = 'images[' + index + '].status';
        this.setData({
            [uploadedImage]: '上传中...'
        });
        wx.uploadFile({
            url: API.getHost() + '/common/uploadImage',
            filePath: filePaths[0],
            name: 'file',
            header: {
                'openId': getApp().globalData.userInfo.openId
            },
            success: (res) => {
                let data = JSON.parse(res.data);
                if (data.success) {
                    that.data.imagesUploaded = this.data.imagesUploaded.concat(data.data.imageUrl);
                    that.setData({
                        [uploadedImage]: '上传完成'
                    });
                } else {
                    that.setData({
                        [uploadedImage]: '上传失败'
                    });
                }
            },
            fail: (res) => {
                that.setData({
                    [uploadedImage]: '上传失败'
                });
            },
            complete: () => {
                filePaths.splice(0, 1);
                if (filePaths.length > 0) {
                    index++;
                    that.batchUpload(filePaths, index);
                } else {
                    if (that.data.detail.content.length >= that.data.contentMinLength) {
                        that.setData({
                            canCreate: true
                        });
                    }
                }
            }
        });
    }
});